package com.itheima.exception;


import com.itheima.pojo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@ResponseBody
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class MyExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R ExceptionHandler(SQLIntegrityConstraintViolationException e){
        log.info("出现字段重复的异常...");
        e.printStackTrace();
        return R.error("对不起,您的用户名重复!");
    }

    @ExceptionHandler(Exception.class)
    public R ExceptionHandler(Exception e){
        log.info("出现了某个未预期的异常...");
        e.printStackTrace();
        return R.error("系统错误,请稍后再试!");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException e){
        log.error(e.getMessage());
        return R.error(e.getMessage());
    }

}
